HEX
Server: Apache/2
System: Linux nexus-01 4.18.0-553.146.1.el8_10.x86_64 #1 SMP Tue Jul 21 03:06:01 EDT 2026 x86_64
User: aglcoke (1118)
PHP: 8.2.32
Disabled: mail,exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname
Upload Files
File: /home/aglcoke/public_html/wp-content/plugins/duplicator-pro/src/Libs/Binary/BinaryIO.php
<?php

/**
 *
 * @package   Duplicator
 * @copyright (c) 2022, Snap Creek LLC
 */

namespace Duplicator\Libs\Binary;

/**
 * BinaryIO class to encode and decode length prefixed binary data.
 * Also uses a special flag to indicate optional data.
 */
class BinaryIO
{
    /**
     * The optional flag.
     * Must be multi-byte, otherwise it is too likely to occur in the binary data
     *
     * @var string
     */
    private const OPTIONAL_FLAG = "<~o>";

    /**
     * Encodes the data with the format given in the constructor. The order is important
     *
     * @param BinaryFormat[] $formats The binary format
     * @param mixed          ...$data The data
     *
     * @return string
     */
    public static function encode(array $formats, ...$data): string
    {
        if (isset($data[0]) && is_array($data[0])) {
            $data = $data[0];
        }

        if (count($data) !== count($formats)) {
            throw new \Exception("Data format mismatch. The data and the format don't match.");
        }

        $data   = array_values($data);
        $result = '';
        foreach ($formats as $i => $f) {
            $v = $data[$i];
            if ($f->isOptional() && ($f->getDefault() === $v || $v === null)) {
                $result .= self::OPTIONAL_FLAG;
            } elseif ($f->isVariableLength()) {
                $result .= pack($f->getFormat(), strlen($v));
                $result .= $v;
            } else {
                $result .= pack($f->getFormat(), $v);
            }
        }

        return $result;
    }

    /**
     * Decodes the binary string into the given format
     *
     * @param BinaryFormat[] $formats The binary format
     * @param string         $binary  The binary string
     *
     * @return array<string, mixed> The data in associative array format
     */
    public static function decode(array $formats, string $binary): array
    {
        $result = [];
        $offset = 0;
        foreach ($formats as $f) {
            $value = '';
            if ($f->isOptional() && substr($binary, $offset, strlen(self::OPTIONAL_FLAG)) === self::OPTIONAL_FLAG) {
                $value   = $f->getDefault();
                $offset += strlen(self::OPTIONAL_FLAG);
            } elseif ($f->isVariableLength()) {
                $length  = unpack($f->getFormat(), $binary, $offset)[1];
                $offset += $f->getSize();
                $value   = substr($binary, $offset, $length);
                $offset += $length;
            } else {
                $value   = unpack($f->getFormat(), $binary, $offset)[1];
                $offset += $f->getSize();
            }

            if ($f->getLabel() !== '') {
                $result[$f->getLabel()] = $value;
            } else {
                $result[] = $value;
            }
        }

        return $result;
    }
}